layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    /**
     * 积分记录管理
     */
    var ScoreLog = {
        tableId: "scoreLogTable"
    };

    /**
     * 初始化表格的列
     */
    ScoreLog.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: ''},
            {field: 'changeScore', sort: true, title: '变动积分'},
            {field: '' +
                    'whereFromName', sort: true, title: '积分类型'},
            {field: 'playerName', sort: true, title: '用户'},
            {field: 'fromPlayerName', sort: true, title: '积分来源'},
            {field: 'adminName', sort: true, title: '代理商'},
            {field: 'orderNo', sort: true, title: '订单号',event:'orderDetail',
                templet:function (d) {
                    return '<span style="color: #01AAED">'+d.orderNo+'</span>'
                }
            },
            {field: 'deptName', sort: true, title: '所属部门'},
            {field: 'updateTime', sort: true, title: '变动时间'},
            {field: 'recordStatus', sort: true,hide:'true', title: '记录状态:1-正常；0-删除；'},
            {align: 'center',hide:'true', toolbar: '#tableBar', title: '操作'}
        ]];
    };


    var updateTimeMin =null;
    var updateTimeMax =null;
    /**
     * 点击查询按钮
     */
    ScoreLog.search = function () {
        var queryData = {};
        ScoreLog.handleData();
        queryData['updateTimeMin'] = updateTimeMin;
        queryData['updateTimeMax'] = updateTimeMax;

        table.reload(ScoreLog.tableId, {where: queryData
            ,page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    };
    ScoreLog.handleData=function ( ) {
        updateTimeMin = $("#updateTimeMin").val();
        updateTimeMax = $("#updateTimeMax").val();
      
        if(!((updateTimeMin==""&&updateTimeMax=="")||(updateTimeMin!="" && updateTimeMax!=""))){
            if(updateTimeMax==""){
                updateTimeMax=Feng.currentDate();
            }
            if(updateTimeMin==""){
                updateTimeMin="2000-01-01"

            }
        }

    }

    laydate.render({
        elem: '#updateTimeMin',
        max: Feng.currentDate()
    });

    laydate.render({
        elem: '#updateTimeMax',
        max: Feng.currentDate()
    });


    /**
     * 弹出添加对话框
     */
    ScoreLog.openAddDlg = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加积分记录',
            content: Feng.ctxPath + '/scoreLog/add',
            end: function () {
                admin.getTempData('formOk') && table.reload(ScoreLog.tableId);
            }
        });
    };

    ScoreLog.openDetailDlg = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '订单详细信息',
            area:['60%','60%'],
            content: Feng.ctxPath + '/order/order_detail?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(ScoreLog.tableId);
            }
        });
    };

    /**
     * 导出excel按钮
     */
    ScoreLog.exportExcel = function () {
        var checkRows = table.checkStatus(ScoreLog.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    ScoreLog.openEditDlg = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改积分记录',
            content: Feng.ctxPath + '/scoreLog/edit?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(ScoreLog.tableId);
            }
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ScoreLog.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/scoreLog/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(ScoreLog.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    //根据单号那订单
    ScoreLog.getOrder=function (data) {
            var  ajax = new $ax(Feng.ctxPath +"/order/getOrderdetail",function (data) {
            ScoreLog.openDetailDlg(data.data);
        }, function (data) {
            Feng.error("订单数据异常!");
        });
        ajax.set("orderNo", data.orderNo);
        ajax.start();
    }



    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ScoreLog.tableId,
        url: Feng.ctxPath + '/scoreLog/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: ScoreLog.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ScoreLog.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ScoreLog.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ScoreLog.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + ScoreLog.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            ScoreLog.openEditDlg(data);
        } else if (layEvent === 'delete') {
            ScoreLog.onDeleteItem(data);
        }else if(layEvent==='orderDetail'){
            //获取订单
            ScoreLog.getOrder(data)

        }
    });
});
